package com.henu.engineer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author noexcept
 * @date 2021/7/23 0:27
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * 拦截所有除登录以外的请求，
     * 对登陆以外的请求进行验证。
     * @param registry
     * @see RequestInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)
     * @see com.henu.engineer.controller.LoginController#Login(HttpSession, HttpServletRequest, String, String) 
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/index.html")
                .excludePathPatterns("/");
    }

    /**
     * 设置主页
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:index.html");
    }
}
